package com.gaff.emp.core.util;

import cn.hutool.core.util.StrUtil;
import com.gaff.emp.core.EmpException;
import inet.ipaddr.IPAddressString;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class IPUtil {

    public static void checkIpFormat(String ipStr) {
        String[] ips = ipStr.split(",");
        for (String ip : ips) {
            IPAddressString ipAddressString = new IPAddressString(ip);
            if (!ipAddressString.isIPAddress()) {
                throw new EmpException("异常的IP格式");
            }
        }
    }

    public static boolean isValid(String ip) {
        IPAddressString ipAddressString = new IPAddressString(ip);
        return ipAddressString.isIPAddress();
    }

    public static boolean isContain(String sourceIp, String targetIp) {
        if (isValid(sourceIp) && isValid(targetIp)) {
            IPAddressString source = new IPAddressString(sourceIp);
            IPAddressString target = new IPAddressString(targetIp);
            return source.getAddress().contains(target.getAddress());
        }
        return false;
    }

    public static String getLocalIP() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp() || netInterface.getDisplayName().toLowerCase().contains("virtual")) {
                    continue;
                } else {
                    Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress ip = addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
            return "Unknow";
        } catch (Exception e) {
            return "Unknow";
        }
    }

    public static String getIPFromRequest() {
        HttpServletRequest request = ContextUtil.getHttpServletRequest();
        if (request == null) {
            throw new EmpException("异常调用");
        }
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StrUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StrUtil.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StrUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StrUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StrUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            throw new EmpException(e.getLocalizedMessage());
        }
        return ip;
    }

}
